﻿/* **
 * WebQ - a Web Questionare / Survey Framework
 * Andrew Adhi Ariane Murniadi / 2009
 * GPL
 * $Id$
 *									** */

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using WebQ.Designer;
using System.Drawing.Design;

namespace WebQ.Core {

	/// <summary>
	/// Collection class for a collection of web entity items. This class will ensure items 
	/// contained in the class have a unique id. The collection also attributed
	/// with a custom editor for working with Visual Studio designer.
	/// </summary>
	[Editor ( typeof ( WebQEntityCollectionEditor ), typeof ( UITypeEditor ) )]
	public class WebQEntityCollection : List<WebQEntity> {

		/// <summary>
		/// Add new unique item. Overrides base implementation by adding uniqueness test
		/// before invoking base Add method.
		/// </summary>
		/// <param name="item">item to be added to the collection</param>
		/// <exception cref="ArgumentException">item with the same id already exists</exception>
		public new void Add ( WebQEntity item ) {
			if (
				this.Exists ( delegate ( WebQEntity en ) {
				return en.QAId == item.QAId;
			} ) ) {

				throw new ArgumentException ( "qa item already exist: " + item.QAItem.Id );
			} else {
				base.Add ( item );
			}
		}

		/// <summary>
		/// Add a range of unique items. Overrides base implementation by adding uniqueness test
		/// before invoking base AddRange method.
		/// </summary>
		/// <param name="collection">items to be added</param>
		/// <exception cref="ArgumentException">at least one item with same id already exists</exception>
		public new void AddRange ( IEnumerable<WebQEntity> collection ) {
			foreach ( WebQEntity en in collection ) {
				if (
					this.Exists ( delegate ( WebQEntity item ) {
					return en.QAId == item.QAId;
				} ) ) {

					throw new ArgumentException ( "qa item already exist: " + en.QAItem.Id );
				}
			}

			base.AddRange ( collection );
		}
	}
}
